package tk.mybatis.springboot.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import tk.mybatis.springboot.api.controller.BaseController;
import tk.mybatis.springboot.model.Message;
import tk.mybatis.springboot.service.PayService;
import tk.mybatis.springboot.share.util.MapHelper;
import tk.mybatis.springboot.share.util.PropertiesUtil;
import tk.mybatis.springboot.wexin.util.HttpUtil;
import tk.mybatis.springboot.wexin.util.ShaUtils;
import tk.mybatis.springboot.wexin.util.UniqId;

@Service
@Transactional
public class PayServiceImpl implements PayService{

	@Override
	public Message gateway(String service, String orderId, String callback_url) throws Exception {
		return gateway(service, "微信公众号支付", orderId, callback_url);
	}
	
	private Message gateway(String service, String orderId, String body, String callback_url) throws Exception {
		Map<String, Object> param = new HashMap<String, Object>();
        param.put("version", "1.0");
        param.put("charset", "UTF-8");
        param.put("sign_type", "MD5");
        param.put("service", service);
        param.put("body", body);
        param.put("attach", "1");
        param.put("sub_openid", "otwWxwM8i5TDzLlnwNgbosHujmsw");
        param.put("out_trade_no", orderId);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        param.put("time_start", formatter.format(new Date()));
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND, 5);
        param.put("time_expire", formatter.format(calendar.getTime()));
        param.put("total_fee", 0.01);
        param.put("mch_id", PropertiesUtil.getMessage("iyibankCode"));
        param.put("notify_url", PropertiesUtil.getMessage("notify_url"));
        param.put("callback_url", callback_url);
    	String decrypt = ShaUtils.getOrderByLexicographic2(param);
    	decrypt += "&key=" + PropertiesUtil.getMessage("iyibankKey");
    	String newstr = DigestUtils.md5Hex(decrypt).toUpperCase();
    	param.put("sign", newstr);
    	String result = HttpUtil.sendPost(PropertiesUtil.getMessage("gatewayUrl"), MapHelper.ToXml(param));
    	Map<String,Object> map = MapHelper.ToMap(result.replaceAll("&", "&amp;"));
    	if("0".equals(map.get("status"))){
    		if("0".equals(map.get("result_code"))){
    			return BaseController.success("成功", map.get("pay_info"));
    		}else{
    			return BaseController.error_msg(map.get("err_msg").toString());
    		}
    	}
    	return BaseController.error_msg("系统繁忙，稍后重试");
	}

	@Override
	public Message refund(String orderId) throws Exception {
		Map<String, Object> param = new HashMap<String, Object>();
        param.put("version", "1.0");
        param.put("charset", "UTF-8");
        param.put("sign_type", "MD5");
        param.put("attach", "1");
        param.put("sub_openid", "otwWxwM8i5TDzLlnwNgbosHujmsw");
        param.put("out_trade_no", orderId);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        param.put("time_start", formatter.format(new Date()));
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND, 5);
        param.put("time_expire", formatter.format(calendar.getTime()));
        param.put("total_fee", 0.01);
        param.put("mch_id", PropertiesUtil.getMessage("iyibankCode"));
        param.put("out_refund_no", UniqId.getUid());
        param.put("total_fee", 0.01);
        param.put("refund_fee", 0.01);
        param.put("op_user_id", PropertiesUtil.getMessage("iyibankCode"));
        String decrypt = ShaUtils.getOrderByLexicographic2(param);
    	decrypt += "&key=" + PropertiesUtil.getMessage("iyibankKey");
    	String newstr = DigestUtils.md5Hex(decrypt).toUpperCase();
    	param.put("sign", newstr);
    	String result = HttpUtil.sendPost(PropertiesUtil.getMessage("refundUrl"), MapHelper.ToXml(param));
    	Map<String,Object> map = MapHelper.ToMap(result.replaceAll("&", "&amp;"));
    	if("0".equals(map.get("status"))){
    		if("0".equals(map.get("result_code"))){
    			return BaseController.success("成功", null);
    		}else{
    			return BaseController.error_msg(map.get("err_msg").toString());
    		}
    	}
    	return BaseController.error_msg("系统繁忙，稍后重试");
	}

}
